package com.xie.demo.demo;

/**
 * @作者：xie
 * @时间：2023/8/17 20:06
 */
public class TABC {

    public static void main(String[] args) {
        TABC tabc = new TABC();
        tabc.printABC();
    }
    private int flag = 0;
    private void printABC() {
        new Thread(() ->{
            for(int i = 0; i < 10; i ++){
                synchronized (TABC.this){
                    while(flag % 3 != 0){
                        try{
                            this.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    System.out.print("A");
                    flag ++;
                    TABC.this.notifyAll();
                }
            }
        }).start();

        new Thread(() ->{
            for(int i = 0; i < 10; i ++){
                synchronized (TABC.this){
                    while(flag % 3 != 1){
                        try{
                            this.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    System.out.print("B");
                    flag ++;
                    TABC.this.notifyAll();
                }
            }
        }).start();


        new Thread(() ->{
            for(int i = 0; i < 10; i ++){
                synchronized (TABC.this){
                    while(flag % 3 != 2){
                        try{
                            this.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    System.out.print("C");
                    flag ++;
                    TABC.this.notifyAll();
                }
            }
        }).start();
    }
}
